package com.centaline.androidsalesblog.fragments.collection;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.activities.sale.EsfEstateDetailActivity;
import com.centaline.androidsalesblog.adapter.EsfEstateAdapter;
import com.centaline.androidsalesblog.api.sale.EsfEstateListApi;
import com.centaline.androidsalesblog.api.usercenter.CollectInfoListApi;
import com.centaline.androidsalesblog.base.BaseCollectFragment;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.constants.SourceType;
import com.centaline.androidsalesblog.events.MineInfoUpdate;
import com.centaline.androidsalesblog.model.databaseobject.EsfEstateDo;
import com.centaline.androidsalesblog.model.responseobject.EsfEstateListRo;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.interfaces.IResponseListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * 小区
 * Created by guilin on 16/3/22.
 */
public class EsfEstateCollFragment extends BaseCollectFragment<EsfEstateDo> {

    private EsfEstateListApi esfEstateListApi;

    @Override
    public void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }

    @Override
    protected void initView() {
        super.initView();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void initApi(CollectInfoListApi collectInfoListApi) {
        collectInfoListApi.setSourceType(SourceType.XIAOQU);
    }

    @Override
    protected void initOtherApi() {
        esfEstateListApi = new EsfEstateListApi(EsfEstateCollFragment.class, EsfEstateListRo.class, new IResponseListener<EsfEstateListRo>() {
            @Override
            public void onResponse(EsfEstateListRo esfEstateListRo) {
                stopRefresh(esfEstateListRo.getList());
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                mRecyclerView.refreshError();
            }
        });
    }

    /**
     * 用户中心数据变化
     */
    @Subscribe
    public void onEventMainThread(MineInfoUpdate mineInfoUpdate) {
        int status = mineInfoUpdate.getStatus() / 10;
        if (status == 1) {
            if (mineInfoUpdate.getStatus() == 13) {
                mRecyclerView.startRefresh();
            }
        }
    }

    @Override
    protected void requestList(String ids) {
        esfEstateListApi.setCestcodes(ids);
        esfEstateListApi.startRequest();
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        return new EsfEstateAdapter(getActivity(), GlideLoad.init(this), list);
    }

    @Override
    protected void ItemClick(EsfEstateDo esfEstateDo) {
        startActivity(new Intent(getActivity(), EsfEstateDetailActivity.class)
                .putExtra(ExtraConstant.CestCode, esfEstateDo.getCestCode()));
    }
}
